(function()
{
	var tabFun = function(options)
	{
		this.id = getRandId();
		this.html = '';
		this.ck = ''; // 当前页面
		if (options)
		{
			// 构造函数
			this.options = $.extend({
				container : 'maincontent',
				initTab : '',
				classname : '',
				height : 0,
				showheader : true,
				onShowTabFun : null, // function(k){}, // 显示某页面时执行
				tablist : {
					tab1 : {
						caption : 'caption1',
						clickThenInit : false,// 点击时执行初始函数(initFun)
						// 初始执行函数，fun为执行函数，key为所在的标签值
						initFun : function(fun, key)
						{
						},
					},
					tab2 : {
						caption : 'caption2',
						initFun : null
					}
				}
			}, options);
			app.templete(this);
			this._setEvent();
			this._show();
		}
	};
	// 根据html对象取得当前
	tabFun.prototype.getThis = function(obj)
	{
		var k = $(obj).parents('.tabbox');
		if (k.length > 0)
		{
			return app.templete(k[0].id);
		}
		return null;
	}
	// 初始化对象
	tabFun.prototype._setEvent = function()
	{
		// 下面只执行一次
		if (tabFun.prototype._)
			return;
		tabFun.prototype._ = 1;
		var that = this;
		// 操作按钮
		$('body').on('click', '.tabbox .tab', function()
		{
			var k = $(this).attr('data');
			var ta = that.getThis(this);
			if (k == ta.ck)
				return;
			if (!ta.initTabOne(k))
			{
				if (ta.options.tablist[k].clickThenInit)
				{
					var fun = ta.options.tablist[k].initFun;
					if (fun)
					{
						ta.isinit[k] = true;
						fun(function(html)
						{
							ta.setData(k, html)
						}, k);
					}
				}
			}
		})
	};
	tabFun.prototype.showTab = function(k)
	{
		var b = $('#' + this.id);
		b.find('.tab-pane.' + this.id).each(function()
		{
			$(this).removeClass('active');
		});
		b.find('#' + this.id + k).each(function()
		{
			$(this).addClass('active');
		});
		b.find('li.' + this.id).each(function()
		{
			$(this).removeClass('active');
		});
		b.find('#tab_' + this.id + k).each(function()
		{
			$(this).addClass('active');
		});
		if (this.options.onShowTabFun)
			this.options.onShowTabFun(k);
	};
	tabFun.prototype.setNeedRefresh = function(refreshCur)
	{
		if (!this.isinit)
			this.isinit = {};
		for ( var k in this.options.tablist)
		{
			this.isinit[k] = false;
		}
		if (refreshCur && this.ck)
			this.initTabOne(this.ck);
	}
	tabFun.prototype.initTabOne = function(k, refresh)
	{
		var that = this;
		this.showTab(k);
		if (!this.isinit)
			this.isinit = {};
		this.ck = k;
		var fun = this.options.tablist[k].initFun;
		if (fun)
		{
			if (!this.isinit[k] || refresh)
			{
				this.isinit[k] = true;
				fun(function(html)
				{
					that.setData(k, html)
				}, k);
				return true;
			}
		}
	};
	tabFun.prototype.setData = function(k, html, notTry)
	{
		// 如果容器不在，则不调入数据
		if ($('#' + this.id).length <= 0)
		{
			return '';
		}
		if (!html)
			html = '';
		if (!k)
			k = this.ck;
		if (arguments.length > 1)
		{
			var kc = $('#' + this.id + ' #' + this.id + k);
			if (kc.length > 0)
				kc.html(html);
		}
		else if (this.options.tablist[k].initFun)
		{
			var fun = this.options.tablist[k].initFun;
			if (fun)
			{
				var that = this;
				return fun(function(html)
				{
					that.setData(k, html);
				}, k);
			}
		}
		return html;
	}
	// 显示tab
	tabFun.prototype._show = function()
	{
		var container = this.options.container;
		var t = '<div class="nav-tabs-custom tabbox ' + this.options.classname + '" id="' + this.id + '" style="';
		if (this.options.height)
			t += 'height:' + this.options.height + 'px;';
		if (container == 'maincontent')
		{
			t += 'min-height:' + app.getMainHeight() + 'px;';
		}
		t += '">';
		if (this.options.showheader)
			t += '<ul class="nav nav-tabs">';
		var i = 0;
		var ck = '';
		ck0 = '';
		for ( var k in this.options.tablist)
		{
			if (!this.options.tablist[k])
				continue;
			var cap = this.options.tablist[k].caption;
			if (i == 0)
				ck0 = k;
			if (this.options.initTab == k)
			{
				ck = k;
			}
			var tit = '';
			if (this.options.tablist[k].title)
				tit = ' title="' + this.options.tablist[k].title + '" ';
			if (this.options.showheader)
				t += '<li id="tab_' + this.id + k + '" class="' + this.id + ' tab" data="' + k + '"><a ' + tit + ' data-toggle="tab" >' + cap + '</a></li>';
			i++;
		}
		if (!ck)
			ck = ck0;
		this.ck = ck;
		if (this.options.showheader)
			t += '</ul>';
		t += '<div class="tab-content">';
		var i = 0;
		for ( var k in this.options.tablist)
		{
			t += '<div class="tab-pane ' + this.id + ' ' + (i == 0 ? 'active' : '') + '" id="' + this.id + k + '">' + (i == 0 ? '<!--' + ck + '-->' : '') + '</div>';
			i++;
		}
		t += '</div></div>';
		t = t.replace('<!--' + ck + '-->', this.setData(ck));
		if (container)
		{
			if (container == 'maincontent')
			{
				if (!app.vm)
					t += '<span class="fa fa-close righttopclose"></span>';
				left.setMainContentHtml(t, null, false, '', null, true);
			}
			else
				$('#' + container).html(t);
		}
		var that = this;
		setTimeout(function()
		{
			that.initTabOne(ck);
			that.showTab(ck);
		}, 100);
		this.html = t;
	}
	window.tabFun = tabFun;
}());
